﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Model;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddExamRoomStudent
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddExamRoomStudent));
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

             
                string ExamRoomId = (string)WebEnvironments.Request(SiteId, "ExamRoomId");
                ExamRoomCls OExamRoom = new ExamRoomBll().CreateModel(SiteId, ExamRoomId);
                if (OExamRoom == null) throw new Exception("Không tìm thấy phòng thi này. Tham số không hợp lệ!");
                string BackUrl = "ExamRoomStudent.aspx?ExamRoomId=" + ExamRoomId;
                string SelectSex = "<select id=drpSex>\r\n" +
                                   "    <option value=\"0\">Nữ</option>\r\n" +
                                   "    <option value=\"1\">Nam</option>\r\n" +
                                   "</select>\r\n";
                string RetHtml =
                    "<script language=javascript>\r\n" +
                    " function ClientSaveAddExamRoomStudent()\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         StudentCode=document.getElementById(\"txtStudentCode\").value;\r\n" +
                    "         StudentName=document.getElementById(\"txtStudentName\").value;\r\n" +
                    "         Dob=document.getElementById(\"txtDob\").value;\r\n" +
                    "         Sex=document.getElementById(\"drpSex\").value;\r\n" +
                    "         Course=document.getElementById(\"txtCourse\").value;\r\n" +
                    "         Class=document.getElementById(\"txtClass\").value;\r\n" +
                    "         TrainingLevel=document.getElementById(\"txtTrainingLevel\").value;\r\n" +
                    "         AjaxOut = Portal.Exam.Render.AddExamRoomStudent.ServerSideAddExamRoomStudent(SiteId, '"+ExamRoomId+"', StudentCode, StudentName, Dob, Sex, Course, Class, TrainingLevel).value;\r\n" +
                    "         if(AjaxOut.Error)\r\n" +
                    "         {\r\n" +
                    "             alert(AjaxOut.InfoMessage);\r\n" +
                    "             return;\r\n" +
                    "         }\r\n" +
                    "         window.open('" + BackUrl+"','_self');\r\n"+
                    "     }\r\n" +

                    "</script>\r\n" +
                " <div class=DatabaseFunction>\r\n" +
                "        <ul>\r\n" +
                "            <li onclick=\"javascript:ClientSaveAddExamRoomStudent();\"><img class=image src=\""+IconRoot+"/Save.png\"><div>Ghi lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('"+BackUrl+"','_self');\"><img class=image src=\""+IconRoot+"/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +
                "   <h3>Thêm thông tin thí sinh</h3>\r\n"+
                "   <div class=CssFormSaveUpdate>\r\n" +
                "     <table style=\"width:99%\">" +
                "          <tr>" +
                "              <td class=\"Col1\">Mã sinh viên:</td>" +
                "              <td><input type=textbox class=textbox id=txtStudentCode></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Tên sinh viên:</td>" +
                "              <td><input class=textbox id=txtStudentName></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Ngày sinh:</td>" +
                "              <td>"+LookUpUtilities.GenerateLookUpDate("txtDob","")+"</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Giới tính:</td>" +
                "              <td>"+SelectSex+"</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Khoá:</td>" +
                "              <td><input type=textbox class=textbox id=txtCourse></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Lớp:</td>" +
                "              <td><input type=textbox class=textbox id=txtClass></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Ngành:</td>" +
                "              <td><input type=textbox class=textbox id=txtTrainingLevel></td>" +
                "          </tr>" +
                "      </table>" +
                "</div>\r\n";


                OAjaxOut.HtmlContent = RetHtml;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage=ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public AjaxOut ServerSideAddExamRoomStudent(
            string SiteId,
            string ExamRoomId,
            string StudentCode,
            string StudentName,
            string SDob,
            string Sex,
            string Course,
            string Class,
            string TrainingLevel)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(StudentCode))
                {
                    throw new Exception("Chưa nhập thông tin mã sinh viên!");
                }
                if (string.IsNullOrEmpty(StudentName))
                {
                    throw new Exception("Chưa nhập thông tin tên sinh viên!");
                }
                if (!FunctionUtilities.checkVnDate(SDob))
                {
                    throw new Exception("Ngày sinh nhập không đúng!");
                }
                if (string.IsNullOrEmpty(Course))
                {
                    throw new Exception("Chưa nhập thông tin khóa!");
                }
                if (string.IsNullOrEmpty(Class))
                {
                    throw new Exception("Chưa nhập thông tin lớp!");
                }
                if (string.IsNullOrEmpty(TrainingLevel))
                {
                    throw new Exception("Chưa nhập thông tin chuyên ngành!");
                }

                DateTime DateOfBirth= FunctionUtilities.VNDateToDate(SDob);
                new ExamRoomStudentBll().CreateExamRoomStudent(
                    null,
                    SiteId,
                    ExamRoomId,
                    StudentCode,
                    StudentName,
                    int.Parse(Sex),
                    DateOfBirth ,
                    Course,
                    Class,
                    TrainingLevel,
                    WebSessionUtility.GetCurrentLoginUser().AssetCode);
                OAjaxOut.InfoMessage = "Sửa chữa thông số thí sinh thành công!";
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage= ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}